<?php
namespace app\components\pay;

use Yii;
use yii\base\Exception;
use app\components\Func;
use app\components\Mcrypt;

/**
 * 商户版微信支付
 */
class WxpayAccept extends WxpayMch {
    
    //预支付
    public function prepay(array $paras) {
        $paras['appid'] = $this->acceptPaymentObj->appid;
        $paras['mch_id'] = $this->acceptPaymentObj->mchid;
        $paras['sub_appid'] = $this->appid;
        $paras['sub_mch_id'] = $this->paymentObj->partner_id;
        $paras['notify_url'] = empty($paras['notify_url']) ? $this->notifyurl() : $paras['notify_url'];
        Yii::$app->redis_java->rPush('QUEUE_WX_PAY_ORDER', json_encode($paras,JSON_UNESCAPED_UNICODE));

        return parent::prepay($paras);
    }
    //根据支付码获取openid
    public function authcodeToOpenid(array $paras) {
        $paras['appid'] = $this->acceptPaymentObj->appid;
        $paras['mch_id'] = $this->acceptPaymentObj->mchid;
        //个别商家没有公众号只用刷卡支付，填写一个特定的公众号，appid固定为appidisnull
        if($this->appid != 'appidisnull')$paras['sub_appid'] = $this->appid;
        $paras['sub_mch_id'] = $this->paymentObj->partner_id;
        return parent::authcodeToOpenid($paras);
    }
    //刷卡支付
    public function micropay(array $paras) {
        $paras['appid'] = $this->acceptPaymentObj->appid;
        $paras['mch_id'] = $this->acceptPaymentObj->mchid;
        //个别商家没有公众号只用刷卡支付，填写一个特定的公众号，appid固定为appidisnull
        if($this->appid != 'appidisnull')$paras['sub_appid'] = $this->appid;
        $paras['sub_mch_id'] = $this->paymentObj->partner_id;
        //Yii::$app->redis_java->rPush('QUEUE_WX_PAY_ORDER', json_encode(($paras + ['notify_url'=>$this->notifyurl()]), JSON_UNESCAPED_UNICODE));
        return parent::micropay($paras);
    }
    //被扫刷卡支付重复查询或执行撤销
    protected function micropayquery(array $paras, $nums = 0){
        $paras['appid'] = $this->acceptPaymentObj->appid;
        $paras['mch_id'] = $this->acceptPaymentObj->mchid;
        //个别商家没有公众号只用刷卡支付，填写一个特定的公众号，appid固定为appidisnull
        if($this->appid != 'appidisnull')$paras['sub_appid'] = $this->appid;
        $paras['sub_mch_id'] = $this->paymentObj->partner_id;
        return parent::micropayquery($paras, $nums);
    }
    //定单查询,$nums调用次数
    public function orderquery(array $paras) {
        $paras['appid'] = $this->acceptPaymentObj->appid;
        $paras['mch_id'] = $this->acceptPaymentObj->mchid;
        //个别商家没有公众号只用刷卡支付，填写一个特定的公众号，appid固定为appidisnull
        if($this->appid != 'appidisnull')$paras['sub_appid'] = $this->appid;
        $paras['sub_mch_id'] = $this->paymentObj->partner_id;
        return parent::orderquery($paras);
    }
    
    /**
     * 微信 退款
     * @param object $orderPay  订单支付数据
     * @param int $refund_fee   退款金额
     * @auth qiaohongbo@kelaile.cn
     */
    
    public function refund(array $paras) {
        $paras['appid'] = $this->acceptPaymentObj->appid;
        $paras['mch_id'] = $this->acceptPaymentObj->mchid;
        //个别商家没有公众号只用刷卡支付，填写一个特定的公众号，appid固定为appidisnull
        if($this->appid != 'appidisnull')$paras['sub_appid'] = $this->appid;
        $paras['sub_mch_id'] = $this->paymentObj->partner_id;
        return parent::refund($paras);
    }
    
    //退款查询
    public function refundquery(array $paras){
        $paras['appid'] = $this->acceptPaymentObj->appid;
        $paras['mch_id'] = $this->acceptPaymentObj->mchid;
        //个别商家没有公众号只用刷卡支付，填写一个特定的公众号，appid固定为appidisnull
        if($this->appid != 'appidisnull')$paras['sub_appid'] = $this->appid;
        $paras['sub_mch_id'] = $this->paymentObj->partner_id;
        return parent::refundquery($paras);
    }
    
    //撤销
    public function cancel(array $paras){
        $paras['appid'] = $this->acceptPaymentObj->appid;
        $paras['mch_id'] = $this->acceptPaymentObj->mchid;
        //个别商家没有公众号只用刷卡支付，填写一个特定的公众号，appid固定为appidisnull
        if($this->appid != 'appidisnull')$paras['sub_appid'] = $this->appid;
        $paras['sub_mch_id'] = $this->paymentObj->partner_id;
        return parent::cancel($paras);
    }
    //获取证书
    public function getPemCert(){
        return $this->paymentObj->getPemCert(isset($this->acceptPaymentObj->mchid) ? $this->acceptPaymentObj->mchid : '');
    }
    public function getPemKey(){
        return $this->paymentObj->getPemKey(isset($this->acceptPaymentObj->mchid) ? $this->acceptPaymentObj->mchid : '');
    }
    public function getJsapi(array $paras) {
        $paras['appid'] = $this->acceptPaymentObj->appid;
        $paras['mch_id'] = $this->acceptPaymentObj->mchid;
        $paras['sub_appid'] = $this->appid;
        $paras['sub_mch_id'] = $this->paymentObj->partner_id;
        if(!isset($paras['openid'])) throw new \Exception('openid为空');
        if(!isset($paras['body'])) throw new \Exception('body为空');
        if(!isset($paras['out_trade_no'])) throw new \Exception('out_trade_no为空');
        if(!isset($paras['total_fee'])) throw new \Exception('total_fee为空');
        if(!isset($paras['notify_url'])) throw new \Exception('notify_url为空');
        $paras['trade_type']='JSAPI';
        $paras['sub_openid'] = $paras['openid'];
        unset($paras['openid']);
        $arr = $this->prepay($paras);

        $jsapi_arr = [];
        $jsapi_arr["appId"] = $this->acceptPaymentObj->appid; //受理Appid
        $jsapi_arr["timeStamp"] = (string)time();
        $jsapi_arr["nonceStr"] = Mcrypt::getcode(32);
        $jsapi_arr["package"] = "prepay_id=".$arr['prepay_id'];
        $jsapi_arr["signType"] = "MD5";
        $jsapi_arr["paySign"] = $this->getSign($jsapi_arr);
        return json_encode($jsapi_arr);
    }
    
}